"use strict";
const redisManager = require('./redisManager');
const CONFIG = require('../../../../../common/config');
const moment = require('moment');
const async = require('async');


const luaUtil = (function () {

    function luaUtil() {
    }


    /**
     * 设置apiKey
     * @author mk
     * @date 2018-5-30
     *
     * @param keyInfo key信息
     * @param expiration token的过期时间
     * @param callback token
     */
    luaUtil.setApiKeyAndToken= function (keyInfo,token,expiration,callback) {
        redisManager.getOpenApiRedisDBMgr(function (getMgrErr, scriptMgr) {
            if(getMgrErr) {
                return callback(getMgrErr);
            }
            let keys = [];
            let values = [];
            let tokenCreateTime = moment().format('YYYY-MM-DD HH:mm:ss');
            values.push(keyInfo.apiKey);
            values.push(keyInfo.auth);
            values.push(keyInfo.setIds);
            values.push(keyInfo.userId);
            values.push(keyInfo.desc);
            values.push(keyInfo.createTime);
            values.push(expiration);
            values.push(token);
            values.push(tokenCreateTime);
            scriptMgr.run('setApiKeyAndToken', keys, values, function (error, result) {
                if(error) {
                    return callback(error,null);
                }
                if (result){
                    return callback(null,token);
                }
                return callback(null,null);
            });
        });
    };


    /**
     * 设置token
     * @author mk
     * @date 2018-5-30
     * @param apiKey 要查询的apikey
     * @param token  要查询的token
     * @param callback 0/1 0表示成功，1表示失败
     */
    luaUtil.queryCurrentToken= function (apiKey,token,callback) {
        redisManager.getOpenApiRedisDBMgr(function (getMgrErr, scriptMgr) {
            if(getMgrErr) {
                return callback(getMgrErr);
            }
            let keys = [];
            let values = [];
            values.push(apiKey);
            values.push(token);
            scriptMgr.run('queryCurrentToken', keys, values, function (error, expireTime) {
                if(error) {
                    return callback(error,null);
                }
                return callback(null,expireTime);
            });
        });
    };


    /**
     * 检测token合法性和权限
     * @author mk
     * @date 2018-5-30
     * @param token 需要验证的token
     * @param callback 0/1 0表token示通过，1表示未通过
     */
    luaUtil.checkToken= function (token,callback) {
        redisManager.getOpenApiRedisDBMgr(function (getMgrErr, scriptMgr) {
            if(getMgrErr) {
                return callback(getMgrErr);
            }
            let keys = [];
            let values = [];
            values.push(token);
            scriptMgr.run('checkToken', keys, values, function (error, result) {
                if(error) {
                    return callback(error,null);
                }
                return callback(null,result);
            });
        });
    };

    /**
     * 获取当前token能够查看的set列表
     * @author mk
     * @date 2018-5-30
     * @param token 需要验证的token
     * @param callback 0/1 0表token示通过，1表示未通过
     */
    luaUtil.getSetIds= function (token,callback) {
        redisManager.getOpenApiRedisDBMgr(function (getMgrErr, scriptMgr) {
            if(getMgrErr) {
                return callback(getMgrErr);
            }
            let keys = [];
            let values = [];
            values.push(token);
            scriptMgr.run('getSetIds', keys, values, function (error, setIds) {
                if(error) {
                    return callback(error,null);
                }
                return callback(null,setIds);
            });
        });
    };


    /**
     * 获取当前token能够查看的userId
     * @author mk
     * @date 2018-5-30
     * @param token 需要验证的token
     * @param callback 0/1 0表token示通过，1表示未通过
     */
    luaUtil.getUserId= function (token,callback) {
        redisManager.getOpenApiRedisDBMgr(function (getMgrErr, scriptMgr) {
            if(getMgrErr) {
                return callback(getMgrErr);
            }
            let keys = [];
            let values = [];
            values.push(token);
            scriptMgr.run('getUserId', keys, values, function (error, userId) {
                if(error) {
                    return callback(error,null);
                }
                return callback(null,userId);
            });
        });
    };



    return luaUtil;
})();

module.exports = luaUtil;